<?php
namespace Api\Controller;
use Think\Controller;
class JobController extends CommonController {
    public function index(){
        $map['wid'] = session('wid');
        $map['status'] = 1;
        $map['type'] = 1;
        $list['job'] = M("Job")->where($map)->order("last_time desc")->limit(5)->select();
        foreach ($list['job'] as $key => $value) {
            if($value['is_top'] == 1 && $value['top_end_time'] < date("Y-m-d H:i:s")){
                $list['job'][$key]['is_top'] = 0;
            }
            $list['job'][$key]['tags'] = explode(',', $value['tag']);
        }
        $map['type'] = 2;
        $list['qiu'] = M("Job")->where($map)->order("last_time desc")->limit(5)->select();
        foreach ($list['qiu'] as $key => $value) {
            if($value['is_top'] == 1 && $value['top_end_time'] < date("Y-m-d H:i:s")){
                $list['qiu'][$key]['is_top'] = 0;
            }
            $list['qiu'][$key]['tags'] = explode(',', $value['tag']);
        }
        $map['type'] = 3;
        $list['jianzhi'] = M("Job")->where($map)->order("last_time desc")->limit(5)->select();
        foreach ($list['jianzhi'] as $key => $value) {
            if($value['is_top'] == 1 && $value['top_end_time'] < date("Y-m-d H:i:s")){
                $list['jianzhi'][$key]['is_top'] = 0;
            }
            $list['jianzhi'][$key]['tags'] = explode(',', $value['tag']);
        }
        $this->ajaxReturn($list);
    }

    public function lists(){
        $map['wid'] = session('wid');
        $map['status'] = 1;
        $map['type'] = $_GET['type'];
        $list = M("Job")->where($map)->order("is_top desc,last_time desc")->select();
        foreach ($list as $key => $value) {
            if($value['is_top'] == 1 && $value['top_end_time'] < date("Y-m-d H:i:s")){
                $list[$key]['is_top'] = 0;
            }
            $list[$key]['tags'] = explode(',', $value['tag']);
        }
        $this->ajaxReturn($list);
    }

    public function item(){
        $map['wid'] = session('wid');
        $map['status'] = 1;
        $map['id'] = $_GET['id'];
        $info = M("Job")->where($map)->find();
        $info['tags'] = explode(',', $info['tag']);
        $this->ajaxReturn($info);
    }

    public function add(){
        foreach ($_POST as $key => $value) {
            if(!is_numeric($value) && empty($value)){
                $arr = array("err_code"=>400,"err_msg"=>"请填写全部信息！");
                $this->ajaxReturn($arr);
            }
        }
        $_POST['wid'] = session('wid');
        $_POST['create_time'] = date("Y-m-d H:i:s");
        $_POST['type'] = $_GET['type'];
        if($info_id = M("Job")->add($_POST)){
            $this->getInfoOrder($info_id);

            $map['id'] = $info_id;
            $data['status'] = 1;
            M("Job")->where($map)->save($data);
            $arr = array("err_code"=>0, "err_msg"=>"发布成功！");
        }else{
            $arr = array("err_code"=>400,"err_msg"=>"网络忙，请稍后再试！");
        }
        $this->ajaxReturn($arr);
    }
}